package com.cosmetics.groupware.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class PropertiesReader
{
	
	/**
	 * Description  : 파일로부터 Properties 를 Load
	 * @Method Name : getProperties
	 * @param propertyFile
	 * @return
	 */
	public Properties getProperties(String propertyFile)
	{
		Properties props = new Properties();
		try
		{
			FileInputStream fis = new FileInputStream(propertyFile);
			props.load(fis);
			fis.close();
			//props.list(System.out);
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return props;
	}

	
	/**
	 * Description  : property 가져오기
	 * @Method Name : getProperty
	 * @param propertyFile
	 * @param key
	 * @return
	 */
	public String getProperty(String propertyFile, String key)
	{
		Properties props = new Properties();
		try
		{
			FileInputStream fis = new FileInputStream(propertyFile);
			props.load(fis);
			fis.close();
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		String result = props.getProperty(key);
		return result;
	}
	
	/**
	 * Description  : property 설정하기
	 * @Method Name : setProperty
	 * @param propertyFile
	 * @param key
	 * @param value
	 */
	public void setProperty(String propertyFile, String key, String value)
	{
		try
		{
			Properties props = new Properties();
			FileInputStream fis = new FileInputStream(propertyFile);
			props.load(fis);
			props.setProperty(key, value);
			props.store(new FileOutputStream(propertyFile), "");
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	
	/**
	 * Description  : 설정값을 Map으로 반환
	 * @Method Name : getPropertiesMap
	 * @param propertyFile
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Map<String, String> getPropertiesMap(String propertyFile)
	{
		Properties props = null;
		Map<String, String> map = null;
		try
		{
			props = getProperties(propertyFile);
			map = new HashMap<String, String> ((Map) props);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return map;
	}
	
	public String getValue(Map<String, String> map, String key)
	{
		String value = null;
		value = map.get(key);
		return value;
	}
	
}  // end of class
